import 'package:elephant_fish/http/core/hi_error.dart';
import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/beInvited_request.dart';
import 'package:elephant_fish/model/beInvited_entity.dart';
import 'package:elephant_fish/model/user_info_entity.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:elephant_fish/widget/welfare_center_list.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:share/share.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_page.dart';

class SharePage extends StatefulWidget {
  final UserInfoEntity userInfoEntity;
  const SharePage({Key key, this.userInfoEntity}) : super(key: key);

  @override
  _SharePageState createState() => _SharePageState();
}

class _SharePageState extends State<SharePage> {

  List<BeInvitedEntity> userInfoEntityList;
  bool _loading = false;
  int pageIndex = 1;

  ScrollController _scrollController = ScrollController();
  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      var dis = _scrollController.position.maxScrollExtent -
          _scrollController.position.pixels;
      //当距离底部不足300时加载更多
      if (dis < 300 && !_loading) {
        _loadData(loadMore: true);
      }
    });
    _loadData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff140c3d),
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Color(0xff140c3d),
        leading: GestureDetector(
          child: Icon(Icons.arrow_back_ios,size: SizeUtil.px(32),color: Color(0xffffffff),),
          onTap: (){
            Navigator.pop(context);
          },
        ),
        title: Text(
          '邀请',style: TextStyle(fontSize: SizeUtil.px(32),color: Color(0xffffffff),),
        ),
        
      ),
      body:
      Column(
        children: [
          Align(
            alignment: Alignment.topLeft,
            child: Container(
              width: double.infinity,
              height: SizeUtil.px(240),
              child: Image.asset('assets/images/share_bg.png'),
            ),
          ),
          SizedBox(height: SizeUtil.px(10),),
          Align(
            alignment: Alignment.topCenter,
            child: Container(
              decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(SizeUtil.px(13))),
                color: Color(0xff2b244d),

              ),
              width: SizeUtil.px(698),
              height: SizeUtil.px(368),
              child: Column(
                children: [
                  Align(
                    alignment: Alignment.topCenter,
                    child: Padding(
                      padding: EdgeInsets.only(top: SizeUtil.px(38)),
                      child: Text('您的邀请码',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),),
                    ),
                  ),
                  SizedBox(height: SizeUtil.px(33),),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      // SizedBox(width: SizeUtil.px(94),),
                      // Container(
                      //   width: SizeUtil.px(53),
                      //   height: SizeUtil.px(53),
                      //   decoration: BoxDecoration(
                      //     border: Border.all(width: SizeUtil.px(2),color: Color(0xffff215d)),
                      //   ),
                      //   child: Center(
                      //     child: Text(
                      //       '6',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(width: SizeUtil.px(22),),
                      // Container(
                      //   width: SizeUtil.px(53),
                      //   height: SizeUtil.px(53),
                      //   decoration: BoxDecoration(
                      //     border: Border.all(width: SizeUtil.px(2),color: Color(0xffff215d)),
                      //   ),
                      //   child: Center(
                      //     child: Text(
                      //       '6',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(width: SizeUtil.px(22),),
                      // Container(
                      //   width: SizeUtil.px(53),
                      //   height: SizeUtil.px(53),
                      //   decoration: BoxDecoration(
                      //     border: Border.all(width: SizeUtil.px(2),color: Color(0xffff215d)),
                      //   ),
                      //   child: Center(
                      //     child: Text(
                      //       '6',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(width: SizeUtil.px(22),),
                      // Container(
                      //   width: SizeUtil.px(53),
                      //   height: SizeUtil.px(53),
                      //   decoration: BoxDecoration(
                      //     border: Border.all(width: SizeUtil.px(2),color: Color(0xffff215d)),
                      //   ),
                      //   child: Center(
                      //     child: Text(
                      //       '6',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(width: SizeUtil.px(22),),
                      // Container(
                      //   width: SizeUtil.px(53),
                      //   height: SizeUtil.px(53),
                      //   decoration: BoxDecoration(
                      //     border: Border.all(width: SizeUtil.px(2),color: Color(0xffff215d)),
                      //   ),
                      //   child: Center(
                      //     child: Text(
                      //       '6',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(width: SizeUtil.px(22),),
                      // Container(
                      //   width: SizeUtil.px(53),
                      //   height: SizeUtil.px(53),
                      //   decoration: BoxDecoration(
                      //     border: Border.all(width: SizeUtil.px(2),color: Color(0xffff215d)),
                      //   ),
                      //   child: Center(
                      //     child: Text(
                      //       '6',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                      //     ),
                      //   ),
                      // ),
                      // SizedBox(width: SizeUtil.px(22),),
                      // Container(
                      //   width: SizeUtil.px(53),
                      //   height: SizeUtil.px(53),
                      //   decoration: BoxDecoration(
                      //     border: Border.all(width: SizeUtil.px(2),color: Color(0xffff215d)),
                      //   ),
                      //   child: Center(
                      //     child: Text(
                      //       '6',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),
                      //     ),
                      //   ),
                      // ),
                      Text(widget.userInfoEntity.userId,style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),)
                    ],
                  ),
                  SizedBox(height: SizeUtil.px(29),),
                  Align(
                    alignment: Alignment.topCenter,
                    child: InkWell(
                  child: Center(
                  child: Text('复制',style: TextStyle(fontSize: SizeUtil.px(21),color: Color(0xff818181)),),
            ),
                onTap: (){
                  Clipboard.setData(ClipboardData(text: widget.userInfoEntity.userId));
                  TxUtils.showToast('已复制', context);
                },
          ),
                  ),
                  SizedBox(height: SizeUtil.px(51),),
                  Align(
                    alignment: Alignment.topCenter,
                    child: InkWell(
                      child: Container(
                        width: SizeUtil.px(517),
                        height: SizeUtil.px(73),
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.all(Radius.circular(SizeUtil.px(43))),
                          color: Color(0xffe62d6c),

                        ),
                        child: Center(
                          child: Text('立即邀请',style: TextStyle(fontSize: SizeUtil.px(28),color: Color(0xfff3f3f3)),),
                        ),
                      ),
                      onTap: (){
                        Share.share('【象鱼直播】http://119.28.131.94/');
                      },
                    ), 
                  ),
                ],
              ),
                
            ),
          ),
          SizedBox(height: SizeUtil.px(30),),
          SizedBox(height: SizeUtil.px(300),
            child: ListView.builder(
                itemCount: userInfoEntityList == null ? 0 : userInfoEntityList.length,
                controller: _scrollController,
                shrinkWrap: true,
                itemBuilder:(context,index){
                  return WelfareCenterList(beInvitedEntity: userInfoEntityList[index],);
                }
            ),
          ),
          SizedBox(height: 19,),
          // Stack(
          //   children: [
          //     Align(
          //       alignment: Alignment.topLeft,
          //       child: Padding(
          //         padding: EdgeInsets.only(left: SizeUtil.px(27)),
          //         child: Text('我的邀请人',style: TextStyle(fontSize: SizeUtil.px(24),color: Color(0xfff3f3f3)),),
          //       ),
          //     ),
          //     Align(
          //       alignment: Alignment.topRight,
          //       child: Padding(
          //         padding: EdgeInsets.only(right: SizeUtil.px(91)),
          //         child: Text('去设置',style: TextStyle(fontSize: SizeUtil.px(24),color: Color(0xfff3f3f3)),),
          //       ),
          //     ),
          //     Align(
          //       alignment: Alignment.topRight,
          //       child: Padding(
          //         padding: EdgeInsets.only(right: SizeUtil.px(27),top: SizeUtil.px(6)),
          //         child:  Icon(Icons.arrow_forward_ios,size: SizeUtil.px(24),color: Color(0xffffffff),),
          //       ),
          //     )
          //   ],
          // ),
          // SizedBox(height: SizeUtil.px(35),),
          // Stack(
          //   children: [
          //     Align(
          //       alignment: Alignment.topLeft,
          //       child: Padding(
          //         padding: EdgeInsets.only(left: SizeUtil.px(27)),
          //         child: Text('邀请奖励',style: TextStyle(fontSize: SizeUtil.px(24),color: Color(0xfff3f3f3)),),
          //       ),
          //     ),
          //     Align(
          //       alignment: Alignment.topRight,
          //       child: Padding(
          //         padding: EdgeInsets.only(right: SizeUtil.px(91)),
          //         child: Text('0',style: TextStyle(fontSize: SizeUtil.px(24),color: Color(0xfff3f3f3)),),
          //       ),
          //     ),
          //     Align(
          //       alignment: Alignment.topRight,
          //       child: Padding(
          //         padding: EdgeInsets.only(right: SizeUtil.px(27),top: SizeUtil.px(4)),
          //         child:  Icon(Icons.arrow_forward_ios,size: SizeUtil.px(24),color: Color(0xffffffff),),
          //       ),
          //     )
          //   ],
          // ),
          // SizedBox(height: SizeUtil.px(35),),
          Stack(
            children: [
              Align(
                alignment: Alignment.topLeft,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(27)),
                  child: Text('邀请须知:',style: TextStyle(fontSize: SizeUtil.px(21),color: Color(0xff818181)),),
                ),
              ),
              Padding(padding: EdgeInsets.only(left: SizeUtil.px(27),right: SizeUtil.px(27),top: SizeUtil.px(33) ),
                child: RichText(
                  text: TextSpan(
                    text: '每个用户都有自己的邀请码，只要您邀请的用户输入您的邀请码， 对方充值时，',style: TextStyle(fontSize: SizeUtil.px(22),color: Color(0xff818181)),
                    children: [
                      TextSpan(
                        text: '您将获得对方充值金币8%的奖励！',style: TextStyle(fontSize: SizeUtil.px(22),color: Color(0xffff215d)),
                        )
                      ]
                  ),

                ),
              ),
            ],
          )
        ],
      ),
    );
  }

  Future<void> _loadData({loadMore = false}) async {
    _loading = true;
    if (!loadMore) {
      pageIndex = 1;
    }
    var currentIndex = pageIndex + (loadMore ? 1 : 0);
    print('loading:currentIndex:$currentIndex');
    try {
      SharedPreferences prefs = await SharedPreferences.getInstance();
      String appToken = prefs.getString('app-token');
      BaseRequest request = BeInvitedRequest();
      request
          .add('page', currentIndex)
          .addHeader('usertoken', appToken);
      var result = await HiNet.getInstance().fire(request);
      if (result['code'] == -1) {
        TxUtils.showErrorToast('登录失效，请重新登录', context);
        Navigator.pop(context);
        Navigator.of(context).push(MaterialPageRoute(
          builder: (context) {
            return LoginPage(
              arguments: {'loginCode': '-1'},
            );
          },
        ));
      } else if (result['code'] == 0) {
        List<Map<String, dynamic>> listMap =
        new List<Map<String, dynamic>>.from(result['data']['data']);
        List<BeInvitedEntity> list = List();
        for (int i = 0; i < listMap.length; i++) {
          list.add(BeInvitedEntity.fromJson(listMap[i]));
        }
        setState(() {
          if (loadMore) {
            if (list.isNotEmpty) {
              //合成一个新数组
              userInfoEntityList = [...userInfoEntityList, ...list];
              pageIndex++;
            }
          } else {
            userInfoEntityList = list;
          }
        });
        Future.delayed(Duration(milliseconds: 1000), () {
          _loading = false;
        });
      }
    } on NeedAuth catch (e) {
      print(e);
    } on HiNetError catch (e) {
      print(e);
    }
  }
}
